from convdiffsolverbase import ConvDiffSolverBase
from dolfin import *

class Galerkin(ConvDiffSolverBase):
    '''Galerkin method. To show how badly stabilization is needed.'''
    def __init__(self):
        self.name = 'Galerkin'
        ConvDiffSolverBase.__init__(self)
        
    def solve(self,problem):
        problemName, mesh, eps, BCValues, b, f, BCIndicators = problem.get_vars()

        V = FunctionSpace(mesh,'CG',1)
        VV = VectorFunctionSpace(mesh,'CG',1)
    
        u = TrialFunction(V)
        varphi = TestFunction(V)

        v = interpolate(b,VV)              # interpolated velocity
        a = eps*inner(nabla_grad(u),nabla_grad(varphi))*dx + inner(v,nabla_grad(u))*varphi*dx
        l = f*varphi*dx

        bcs = [DirichletBC(V,value,where) for value, where in zip(BCValues, BCIndicators)]

        A,L = assemble_system(a,l,bcs)

        u = Function(V)
        solve(A,u.vector(),L)
        
        plot(u,interactive=True)
        self.save(u,problem)

